#include "tsg.h"
#include "keyframeAnimation.h"
//#include <iostream>

step::step()
{
	this->position=0;
	this->time=0;
}

step::step(float t, float p)
{
	this->position=p;
	this->time=t;
}

CKeyframe::CKeyframe() : time(this,0)
{
	this->easyinout=false;
}
void CKeyframe::addKeyframe(step* s)
{
	int i=0;
	bool add=false;
	// if i < to the size of the vector and the keyframe has not been added
	while (i<this->vectorStep.size() && add==false)
	{
		//if the time of the keyframe in the vector is > to the time of the new keyframe
		if(this->vectorStep[i].time>s->time )
		{
			vectorStep.insert(vectorStep.begin()+i,(*s));
			add=true;
		}
		i++;
	}
	//if the keyframe has not been added we put it in the end of the vector
	if(add==false)
	{
		this->vectorStep.push_back((*s));
	}
	
}
void CKeyframe::doUpdate()
{
	float s=0;
	float p=0;
	int i=0;
	bool found = false;
	

	
	
	if (time>= vectorStep[0].time && time<vectorStep[vectorStep.size()-1].time)
	{
		while(i<vectorStep.size() && found==false)
		{
			if(vectorStep[i].time>time)
			{
				found=true;
			}
			else
			{
				i++;
			}
		}
		//i is now the index of the second keyframe
		s = (time-vectorStep[i-1].time)/(vectorStep[i].time-vectorStep[i-1].time);
		if(easyinout==true)
		{
			s=3*s*s-2*s*s*s;
		}
		p = vectorStep[i-1].position+ s*(vectorStep[i].position-vectorStep[i-1].position);
		setValue(p);
	}
}